package com.example.abc.web.account;

import com.example.abc.common.Constants;
import com.example.abc.common.RestResponse;
import com.example.abc.entity.Account;
import com.example.abc.exception.ClientException;
import com.example.abc.exception.ServerException;
import com.example.abc.service.AccountService;
import com.example.abc.util.JsonUtil;
import com.example.abc.web.IndexController;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by Peter on 2018/4/24.
 */
@Controller
public class LoginController {
    public static final Logger logger = LoggerFactory.getLogger(IndexController.class);
    private static final List<String> bgNames = Arrays.asList("bg1.jpg", "bg2.jpg", "bg3.jpg", "bg4.jpg", "bg5.jpg", "bg6.jpg", "bg7.jpg");

    @Autowired
    private AccountService accountService;

    // ajax调用此方法，查看http code
    @RequestMapping(value = "/logined", method = {RequestMethod.HEAD})
    @ResponseBody
    public String logined(Model model) {
        return "ok";
    }

    @RequestMapping(value = "/login", method = {RequestMethod.GET})
    public String login(Model model) {
        Random r = new Random();
        int jsNumber = r.nextInt(8) + 1;
        model.addAttribute("jsNumber", jsNumber);

        String bgName = bgNames.get(r.nextInt(7));
        model.addAttribute("bgName", bgName);
        return "login";
    }

    @RequestMapping(value = "/login-error", method = {RequestMethod.POST, RequestMethod.GET})
    public String loginError(Model model) {
        logger.info("login-error");
        model.addAttribute("error", "login error.");
        return "login";
    }

    @RequestMapping(value = "/register", method = {RequestMethod.POST})
    @ResponseBody
    public RestResponse register(@ModelAttribute("account") Account account, Model model) {
        logger.info(Constants.prefix_in + JsonUtil.toJson(account));
        RestResponse restResponse = null;
        try {
            if(StringUtils.isBlank(account.getUsername())){
                throw new ClientException("username can not be null");
            }
            if(StringUtils.isBlank(account.getPassword())){
                throw new ClientException("password can not be null");
            }
            accountService.register(account);
            restResponse = RestResponse.getSuccessResponse(account);
        } catch (ClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (ServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        return restResponse;
    }


}
